"""
-*- coding:utf-8 -*-
最终版web自动化
Author: Free
DataTime: 2020/7/1 17:49

"""


from WebSite.tools import ReadConfig
from time import sleep
from WebSite.PubilcTask import TaskPublic


class LoginRegister(TaskPublic.TaskPublic):

    def forget_code(self):
        """
        忘记密码验证码滑动
        :return:
        """
        self.next_step()
        input("请进行滑动：\n")

    def regiser_code(self):
        """
        注册密码验证码滑动
        :return:
        """
        input("请进行滑动：\n")
        # for i in range(100):
        #     self.next_step()
        #     sleep(2)
        #     element = self.b.find_element_by_css_selector('#mpanel1 > div.verify-bar-area > div > div')
        #
        #     print("开始进行滑动")
        #     ActionChains(self.b).click_and_hold(on_element=element).perform()
        #     ActionChains(self.b).move_to_element_with_offset(to_element=element, xoffset=43,
        #                                                      yoffset=0).perform()
        #     sleep(0.8)
        #     ActionChains(self.b).release().perform()
        #     sleep(1)
        #     print("滑动重试%d次数" % i)
        #     # print(self.b.find_element_by_css_selector('body > div.toast.toast-error').text)
        #     # print(self.b.find_element_by_css_selector('body > div.toast.toast-success').text)
        #     if self.b.find_element_by_css_selector('body > div.toast.toast-error').text in '验证失败':
        #         sleep(1)
        #         print(self.b.find_element_by_css_selector('body > div.toast.toast-error').text)
        #         self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.slideBarBigBoxW > div > div.slideBarBoxX > img').click()
        #         continue
        #     elif self.b.find_element_by_css_selector('body > div.toast.toast-error').text not in '验证失败':
        #         sleep(1)
        #         print("验证成功")
        #         break

    def register_pc(self):
        """
        第一个注册
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div.homeBannerAndHeadBox > div.homeHeadBox > div > div > ul > li:nth-child(1) > div.registerBtn').click()
            sleep(2)
            print("首页注册按钮点击成功")
        except Exception as e:
            print("首页注册按钮点击失败", e)

    def register_tel(self, tel=None):
        """
        注册中手机号填写
        :param tel: 默认为None,可以填写
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.regMsgBox > div:nth-child(1) > div.regMsgInput > input').send_keys(tel)
            sleep(1)
            print("输入手机号成功")
        except Exception as e:
            print("输入手机号成功", e)

    def register_password(self, password=None):
        """
        注册中密码填写
        :param password: 默认为None,可以填写
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.regMsgBox > div:nth-child(2) > div.regMsgInput > input').send_keys(password)
            sleep(1)
            print("注册密码成功")
        except Exception as e:
            print("注册密码失败", e)

    def register_password_again(self, password=None):
        """
        注册中确认密码填写
        :param password: 默认为None,可以填写
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.regMsgBox > div:nth-child(3) > div.regMsgInput > input').send_keys(password)
            sleep(1)
            print("注册确认密码成功")
        except Exception as e:
            print("注册确认密码失败", e)

    def register_finally(self):
        """
        注册最终的
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.regBtnBox > button').click()
            sleep(1)
            print("点击下一步成功")
        except Exception as e:
            print("点击下一步失败", e)

    def register_finally2(self):
        # TODO 注意：新增加部分，是验证码输入窗口的验证按钮，把此按钮的点击从register_message分离出来了
        """
        注册最终的输入验证码验证页面的验证
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.verifyBtnBox > button').click()
            sleep(1)
            print("注册成功")
        except Exception as e:
            print("注册失败", e)


    def register_modify_the_nationality(self):
        """
        注册修改国籍为菲律宾
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.regMsgBox > div:nth-child(1) > div.regMsgInput > div.districtSelectBox > div.selectDistrictTopBox').click()
            sleep(1)
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.regMsgBox > div:nth-child(1) > div.regMsgInput > div.districtSelectBox > div.districtChoiceBigBoxBig > div > div.districtChoiceBox > div:nth-child(163)').click()
            sleep(1)
            print("修改国籍成功")
        except:
            print("修改国籍失败")

    def register_message(self):
        """
        注册验证码输入并点击
        :return:
        """
        self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.verifyContentBox > div.verifyInputBox > div.verifyInput > input').send_keys('请输入注册验证码：')
        self.assertEqual(self.b.current_url, ReadConfig.url_register.value, '注册失败')

    def forget_password(self):
        """
        忘记密码按钮
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div.bigBox > div:nth-child(2) > p > span').click()
            sleep(1)
            print("正在点击忘记密码")
        except Exception as e:
            print("点击忘记密码失败", e)

    def forget_password_user(self, user=None):
        """
        忘记密码输入用户名
        :param user: 默认为空
        :return:
        """
        try:
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.verifyContentBox > div.verifyInputBox > div.verifyInput > input').send_keys(user)
            sleep(1)
            print("忘记密码输入用户名成功")
        except Exception as e:
            print("忘记密码输入用户名失败", e)

    def next_step(self):
        """
        点击下一步
        :return:
        """
        self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.mainPieceFoot > button:nth-child(2)').click()
        sleep(1)

    def message_code(self):
        """
        短信验证码输入，需要手动输入，并进行点击下一步
        :return:
        """
        try:
            print("进行验证码输入")
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.verifyContentBox > div.verifyInputBox > div.verifyInput > input').send_keys(input("请输入验证码：\n"))
            sleep(2)
            self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.mainPieceFoot > button:nth-child(4)').click()
        except Exception as e:
            print("验证码输入并点击错误")

    def forget_password_first(self, password=None):
        """
        输入第一遍密码
        :return:
        """
        print("输入第一遍密码")
        self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.verifyContentBox > div:nth-child(1) > div.regMsgInput > input').send_keys(password)
        sleep(1)

    def forget_password_again(self, password=None):
        """
        输入确认密码
        :return:
        """
        print("输入确认密码")
        self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.verifyContentBox > div:nth-child(2) > div.regMsgInput > input').send_keys(password)
        sleep(1)

    def forget_password_nextStep(self):
        """
        忘记密码提交按钮点击
        :return:
        """
        self.b.find_element_by_css_selector('body > div.appBigBox > div.appbox > div > div:nth-child(2) > div > div > div.mainPieceFoot > button:nth-child(5)').click()
        sleep(1)
        if self.b.current_url == ReadConfig.url_forget.value:
            assert '输入的用户名密码不一致'
        elif self.b.current_url == ReadConfig.url_login.value:
            print("修改密码成功，请进行登录")
            assert '修改密码成功，请进行登录'


